import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:xingsu/net/apis.dart';
import 'package:xingsu/net/http_manager.dart';

class TaskSearchViewModel extends GetxController {
  final list = [].obs;
  final TextEditingController controller = TextEditingController();

  List allList = [];

  @override
  void onInit() {
    super.onInit();
    loadAllData();
  }

  void loadAllData() async {
    String jsonData = await rootBundle.loadString('assets/json/recommend.json');
    allList.assignAll(json.decode(jsonData));
  }

  void search() {
    if (controller.text.isEmpty) return;

    List searchList = [];
    for (Map map in allList) {
      Map task_info = map["task_info"] ?? {};
      String task_name = task_info["task_name"] ?? "";

      if (task_name.contains(controller.text)) {
        searchList.add(map);
      }
    }

    list.assignAll(searchList);
  }
}
